package com.wangrui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wangrui.utils.Page;
import com.wangrui.dto.UserSchool;
import com.wangrui.pojo.User;
import com.wangrui.mapper.UserMapper;
import com.wangrui.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王锐
 * @since 2020-07-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<User> queryAllUser() {
        return  userMapper.selectList(null);
    }

    @Override
    public int saveUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int getTotal() {
        return  userMapper.selectCount(null);// 查询结果数;
    }

    @Override
    public List<UserSchool> getUserSchool(Page<UserSchool> page1) {
        List<UserSchool> userSchools = userMapper.getUserSchool(page1);
        return userSchools;
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUserByIds(int ids) {
        return userMapper.deleteById(ids);
    }

    @Override
    public Boolean queryUserByUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            return true;
        }
        return false;
    }

    @Override
    public User selectUser(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<User> userQueryJson() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","truename");
        return userMapper.selectList(queryWrapper);
    }

}
